#!/bin/bash
# Forward Notification to Event Console

# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# This notification plugin sends a notification to the
# Checkmk Event Console. It takes two parameters:
# 1. Syslog facility to use. This must be a number
#  from 0 to 23. 16 means 'local0', 17 means 'local1',
#  ... and 23 means 'local7'. If you leave this empty
# the local0 is being used.
# 2. IP-Address of the remote Event Console. If you
# do not leave this empty then the notifications are
# being sent via syslog/UDP (port 514) to that address.

# Default values
FACILITY=16
REMOTE=""

# Old-style parameters
if [ "$NOTIFY_PARAMETER_1" ]; then FACILITY="${NOTIFY_PARAMETER_1}"; fi
if [ "$NOTIFY_PARAMETER_2" ]; then REMOTE="${NOTIFY_PARAMETER_2}"; fi

# New-style parameters
if [ -n "$NOTIFY_PARAMETER_FACILITY" ]; then FACILITY="${NOTIFY_PARAMETER_FACILITY}"; fi
if [ -n "$NOTIFY_PARAMETER_REMOTE" ]; then REMOTE="${NOTIFY_PARAMETER_REMOTE}"; fi

if [ "$NOTIFY_WHAT" = HOST ]; then
    # supression: It's been like that; I am not sure. Never touch a running system.
    # shellcheck disable=SC2086 #  Double quote to prevent globbing and word splitting.
    mkevent -n "$FACILITY" "$REMOTE" $NOTIFY_HOSTSTATEID "$NOTIFY_HOSTNAME" "" "$NOTIFY_HOSTOUTPUT" "$NOTIFY_HOST_SL" "$NOTIFY_HOST_EC_CONTACT"
else
    # supression: It's been like that; I am not sure. Never touch a running system.
    # shellcheck disable=SC2086 #  Double quote to prevent globbing and word splitting.
    mkevent -n "$FACILITY" "$REMOTE" $NOTIFY_SERVICESTATEID "$NOTIFY_HOSTNAME" "$NOTIFY_SERVICEDESC" "$NOTIFY_SERVICEOUTPUT" "$NOTIFY_SERVICE_SL" "$NOTIFY_SERVICE_EC_CONTACT" "$NOTIFY_HOST_SL" "$NOTIFY_HOST_EC_CONTACT"
fi
